package com.zzyl.controller.manager;

import cn.hutool.core.lang.UUID;
import com.zzyl.base.ResponseResult;
import com.zzyl.file.FileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Slf4j
@RestController
@RequestMapping("/common")
@Api(tags = "通用接口")
public class CommonController {


    @Autowired
    FileStorageService fileStorageService;

    @SneakyThrows  // lombok注解，用于捕获异常,将方法整体包裹在try-catch中
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public ResponseResult<String> upload(MultipartFile file) throws IOException {
        log.info("文件上传:{}", file);
        //String originalFilename = file.getOriginalFilename(); // 获取上传的文件的原始文件名
        InputStream inputStream = file.getInputStream(); // 获取上传的文件的输入流
        String url = fileStorageService.store(UUID.randomUUID().toString(), inputStream); // 上传文件到 OSS 并返回文件访问路径 UUID.randomUUID().toString()生成一个随机的文件名
        return ResponseResult.success(url);
    }
}
